package cn.itcast.web.controller.mill;

import cn.itcast.domain.cargo.Factory;
import cn.itcast.service.mill.MillService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("mill")
public class MillController extends BaseController{


	@Reference
	MillService millService;


	@RequestMapping(value = "list",name = "查询所有厂商")
	public String findAll(
		@RequestParam(defaultValue = "1")int page,
		@RequestParam(defaultValue = "5")int size){

		//企业的id应该是当前登陆者所在的公司id,暂时写死1
		//String companyId = "1";

		PageInfo pageInfo = millService.findByPage(page, size);


		request.setAttribute("page",pageInfo);
		return "mill/mill-list";
	}

	@RequestMapping(value = "toAdd",name = "跳转到新增厂商页面")
	public String toAdd(){

		return "mill/mill-add";
	}

	@RequestMapping(value = "edit",name = "添加或更新厂商")
	public String edit(Factory factory){


		//判断有无id,做新增或者更新操作
		if (StringUtils.isBlank(factory.getId())) {
			//保存
			millService.save(factory);
		}else{
			//更新
			millService.update(factory);
		}

		return "redirect:/mill/list.do";
	}

	@RequestMapping(value = "toUpdate",name = "跳转到更新厂商的页面")
	public String toUpdate(String id){

		//根据id查询厂商的信息,将这些信息放入域中,在update页面回显
		Factory factory = millService.findById(id);
		request.setAttribute("factory",factory);
		return "mill/mill-update";
	}

	@RequestMapping(value = "delete",name = "删除厂商")
	public String deleteById(String id){

		millService.deleteById(id);

		return "redirect:/mill/list.do";
	}
}
